import time
from appium import webdriver
from appium.options.android import UiAutomator2Options

from config import *


def app_driver(des_caps=Android12):
    # 初始化设备配置信息
    option = UiAutomator2Options().load_capabilities(des_caps)
    # 创建驱动对象
    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=option)
    driver.implicitly_wait(3)
    # 返回app驱动对象
    return driver

def driver_quit(driver):
    # 暂停3秒
    time.sleep(3)
    # 关闭驱动
    driver.quit()

def app_install_remove(driver, app_path=app_path, app_package=package_name):
    if driver.is_app_installed(app_package):
        driver.remove_app(app_package)
    else:
        driver.install_app(app_path)

if __name__ == '__main__':
    driver = app_driver()
    app_install_remove(driver)